package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 *
 * @author LiuLi
 * @date 2025-08-20
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private WechatService wechatService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询老人家属
     *
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id) {
        return familyMemberMapper.selectById(id);
    }

    /**
     * 查询老人家属列表
     *
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember) {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember) {
        return familyMemberMapper.insert(familyMember);
    }

    /**
     * 修改老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember) {
        return familyMemberMapper.updateById(familyMember);
    }

    /**
     * 批量删除老人家属
     *
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids) {
        return familyMemberMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人家属信息
     *
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id) {
        return familyMemberMapper.deleteById(id);
    }

    /**
     * 登录
     *
     * @param userLoginRequestDto 用户登录请求DTO
     * @return 登录结果
     */
    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //1.根据前端传递的code获取openId
        String openid = wechatService.getOpenid(userLoginRequestDto.getCode());
        //2.根据openId查询用户信息
        FamilyMember member = this.lambdaQuery()
                .eq(FamilyMember::getOpenId, openid)
                .one();
        //3.如果用户不存在,则创建用户
        if (member == null) {
            member = new FamilyMember();
            member.setOpenId(openid);
        }
        //4.调用微信接口获取手机号
        String phone = wechatService.getPhone(userLoginRequestDto.getPhoneCode());
        member.setPhone(phone);
        //5.更新用户信息
        //用hutool工具类从配置文件中获取随机的一个昵称前缀
        String nickname = Constants.DEFAULT_NICKNAME_PREFIX.get(RandomUtil.randomInt(0, Constants.DEFAULT_NICKNAME_PREFIX.size()));
        member.setName(nickname + phone.substring(phone.length() - 4));
        //6.保存用户信息
        saveOrUpdateFamilyMember(member);
        //7.封装loginVo
        LoginVo loginVo = new LoginVo();
        Map<String, Object> claims = new HashMap<>();
        claims.put("memberId", member.getId());
        claims.put("memberName", member.getRemark());
        String token = tokenService.createToken(claims);
        loginVo.setToken(token);
        loginVo.setNickName(member.getName());
        //8.返回结果
        return loginVo;
    }

    /**
     * 保存或更新用户信息
     *
     * @param member 用户信息
     */
    private void saveOrUpdateFamilyMember(FamilyMember member) {
        if (member.getId() == null) {
            save(member);
        } else {
            updateById(member);
        }
    }
}
